package com.library.web.controller.api;

import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.TableDataInfo;
import com.library.domain.BoBook;
import com.library.domain.BoBookType;
import com.library.service.IBoBookService;
import com.library.service.IBoBookTypeService;
import com.library.web.controller.vo.BookModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author julyWhj
 * @Description $
 * @Date 2022/3/28 10:46 下午
 **/
@RestController
@RequestMapping("/api/book")
public class BookApiController extends BaseController {
    /**
     * 图书管理Service类
     */
    @Autowired
    private IBoBookService boBookService;
    @Autowired
    private IBoBookTypeService boBookTypeService;

    /**
     * 获取全部分类
     *
     * @return
     */
    @GetMapping("/getTypes")
    public AjaxResult getAll() {
        List<BoBookType> boBookTypes = boBookTypeService.selectBoBookTypeList(new BoBookType());
        List<Map<String, Object>> types = new ArrayList<>();
        Map<String, Object> all = new HashMap<>();
        all.put("name", "全部");
        all.put("id", "all");
        types.add(all);
        boBookTypes.forEach(type -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", type.getTypeName());
            map.put("id", type.getId());
            types.add(map);
        });
        return AjaxResult.success(types);
    }


    @GetMapping("/list")
    public TableDataInfo list(BoBook boBook) {
        startPage();
        List<BoBook> list = boBookService.selectBoBookList(boBook);
        return getDataTable(toModelList(list));
    }

    private List<BookModel> toModelList(List<BoBook> list) {
        List<BookModel> bookModelList = new ArrayList<>();
        list.forEach(boBook -> bookModelList.add(BookModel.toModel(boBook)));
        return bookModelList;
    }

}
